package com.wuliu.dao.impl;

import com.wuliu.dao.FirstImageDAO;
import com.wuliu.model.FirstImage;
import com.wuliu.util.Jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class FirstImageDAOImpl implements FirstImageDAO {
    @Override
    public void add(FirstImage firstImage) {
        try {
            Jdbc jdbc = new Jdbc();
            Connection connection=jdbc.connection();
            String sql="insert into first_image values(null,?)";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,firstImage.getImg());
            System.out.println(firstImage);
            statement.execute();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteById(int id) {
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="delete from first_image where id=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setInt(1,id);
            statement.execute();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void update(FirstImage firstImage) {
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="update first_image set img=? where id=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,firstImage.getImg());
            statement.setInt(2,firstImage.getId());
            statement.execute();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public FirstImage findById(int id) {
        FirstImage firstImage=null;
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="select * from first_image where id=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setInt(1,id);
            ResultSet set =statement.executeQuery();
            while (set.next()){
                String img=set.getString("img");
                firstImage=new FirstImage(id,img);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return firstImage;

    }

    @Override
    public List<FirstImage> findAll() {
        List<FirstImage> firstImages=new ArrayList<>();
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="select * from first_image";
            PreparedStatement statement=connection.prepareStatement(sql);
            ResultSet set=statement.executeQuery();
            while (set.next()){
                int id=set.getInt("id");
                String img=set.getString("img");
                FirstImage firstImage=new FirstImage(id,img);
                firstImages.add(firstImage);
            }
            set.close();
            statement.close();
            connection.close();
        } catch (Exception e) {
                e.printStackTrace();
        }

            return firstImages;
    }



}

